<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class provides access to data layer.
 *
 * @package Aurorum
 * @subpackage Models
 */

class ADS_DataSourceModel extends ADS_DefaultModel {
    
    public function countSources() {
        
        switch(func_num_args()){
            case 1:
                $userId = (int)func_get_arg(0);
                return dibi::fetch('
                  SELECT
                    COUNT(*) as cnt
                  FROM
                    [:wp:ads_datasources] AS t1
                  INNER JOIN
                    [:wp:ads_user_rights] AS t2
                  ON
                    t1.sourceId = t2.assetId
                  WHERE
                    userId = %i AND user_right = %s', $userId, 'R',
                  '%if', (bool)$this->useFilter, ' AND %and', $this->filter
                )->cnt;
            break;                
        }
        
        return dibi::fetch('
          SELECT
            COUNT(*) as cnt
          FROM
            [:wp:ads_datasources] AS t1
          %if', (bool)$this->useFilter, 'WHERE %and', $this->filter
        )->cnt;
    }
    
    public function getSources() {
        
        switch(func_num_args()){
            case 1:
                $userId = (int)func_get_arg(0);
                return dibi::fetchAll('
                  SELECT
                    t1.*              
                  FROM
                    [:wp:ads_datasources] AS t1
                  INNER JOIN
                    [:wp:ads_user_rights] AS t2
                  ON
                    t1.sourceId = t2.assetId
                  WHERE
                    userId = %i AND user_right = %s', $userId, 'R',
                  '%if', (bool)$this->useFilter, ' AND %and', $this->filter, '%end',
                  '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
                  '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
                );
            break;                
        }
        
        return dibi::fetchAll('
          SELECT
            t1.sourceId,
            t1.name,
            t1.content,
            t1.type
          FROM
            [:wp:ads_datasources] AS t1
           %if', (bool)$this->useFilter, 'WHERE %and', $this->filter, '%end',
          '%if', (bool)$this->useOrderBy, 'ORDER BY %by', $this->orderBy, '%end',
          '%if', (bool)$this->useLimit, '%lmt %ofs', $this->limit, $this->offset
        );
    }
    
    public function getSource($sourceId) {
        if(!is_int($sourceId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        return dibi::fetch('
          SELECT
            t1.sourceId,
            t1.name,
            t1.content,
            t1.type
          FROM
            [:wp:ads_datasources] AS t1
          WHERE
            sourceId = %i
        ', $sourceId);
    }    
    
    public function addSource($source){
        if(!is_array($source)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }
        dibi::query('INSERT INTO [:wp:ads_datasources]', $source);
    }
    
    public function editSource($sourceId, $source){
        if(!is_array($source) || !is_int($sourceId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }

        dibi::query('UPDATE [:wp:ads_datasources] SET', $source, 'WHERE sourceId = %i', $sourceId);
    }
    
    public function removeSource($sourceId){        
        if(!is_int($sourceId)){
            throw new InvalidArgumentException(sprintf('Wrong parameters for method %s.',__METHOD__));
        }

        dibi::query('DELETE FROM [:wp:ads_datasource_items] WHERE sourceId = %i', $sourceId);
        dibi::query('DELETE FROM [:wp:ads_datasources] WHERE sourceId = %i LIMIT 1', $sourceId);        
        dibi::query('DELETE FROM [:wp:ads_dataframe_items] WHERE sourceId = %i LIMIT 1', $sourceId);        
    }

}
